package com.xintonghua.bussiness.ui.user;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.alibaba.fastjson.JSONObject;
import com.xintonghua.base.widget.MyItemTextView;
import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.api.manager.AuthManager;
import com.xintonghua.bussiness.base.BaseActivity;
import com.xintonghua.bussiness.bean.AuthInfoBean;
import com.xintonghua.bussiness.bean.StaffBean;
import com.xintonghua.bussiness.ui.user.manage.PositionActivity;
import com.xintonghua.bussiness.util.GlideUtils;
import com.xintonghua.bussiness.util.ImageTools;
import com.xintonghua.bussiness.util.ImageUtils;
import com.xintonghua.bussiness.util.MyState;
import com.xintonghua.bussiness.util.MyUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.addapp.pickers.listeners.OnItemPickListener;
import cn.addapp.pickers.picker.SinglePicker;
import me.iwf.photopicker.PhotoPicker;

/**
 * 作者：wanxianyi
 * 时间：2018/1/15.
 * 包名：com.xintonghua.bussiness.ui.user
 */

public class UserInfoActivity extends BaseActivity {
    @BindView(R.id.iv_head)
    ImageView ivHead;
    @BindView(R.id.edt_username)
    EditText edtUsername;
    @BindView(R.id.edt_dian)
    EditText edtDian;
    @BindView(R.id.edt_work)
    MyItemTextView edtWork;
    @BindView(R.id.edt_work_year)
    EditText edtWorkYear;
    @BindView(R.id.edt_speciality)
    EditText edtSpeciality;
    @BindView(R.id.edt_dream)
    EditText edtDream;
    @BindView(R.id.edt_wx)
    EditText edtWx;
    @BindView(R.id.edt_qq)
    EditText edtQq;
    @BindView(R.id.edt_address)
    EditText edtAddress;
    @BindView(R.id.utils_sex)
    MyItemTextView utilsSex;
    private List<String> pList = new ArrayList<>();
    private int sex = 0;
    private String imagePath;
    private int position_id = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        onSimpleActionBar();
        setSimpleActionBar("我的信息", "保存", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                save();
            }
        });
        httpCent.userInfo(AuthManager.getId(), this, MyState.requestOne);
        initUI();

    }

    @Override
    public void initUI() {
        super.initUI();
        pList.add("男");
        pList.add("女");
    }

    @Override
    public void dataBack(Object data, int order) {
        super.dataBack(data, order);
        switch (order) {
            case MyState.requestOne:
                AuthInfoBean bean = JSONObject.parseObject(data.toString(), AuthInfoBean.class);
                if (bean != null) {
                    imagePath = bean.getImg();
                    sex = bean.getSex();
                    position_id = bean.getPosition_id();
                    GlideUtils.loadCricle(this, ivHead, bean.getImg(), R.drawable.default_head);
                    edtUsername.setText(bean.getName());
                    utilsSex.setMsg(bean.getSex() == 0 ? "女" : "男");
                    edtDian.setText(bean.getShopName());
                   // edtWork.setMsg(bean.getWorking_life());
                    edtWorkYear.setText(bean.getWork_year() + "年");
                    edtSpeciality.setText(bean.getMy_speciality());
                    edtDream.setText(bean.getMy_dream());
                    edtWx.setText(bean.getWx());
                    edtQq.setText(bean.getQq());
                    edtAddress.setText(bean.getAddress());

                }
                break;
            case MyState.requestTwo:
                imagePath = (String) data;
                GlideUtils.loadCricle(this, ivHead, imagePath);
                break;
        }
    }

    /**
     * 保存用户信息
     */
    private void save() {
        if (TextUtils.isEmpty(imagePath)) {
            mToast("上传头像不能为空！");
            return;
        }
        if (TextUtils.isEmpty(getStr(edtUsername))) {
            mToast("姓名不能为空！");
            return;
        }
        if (TextUtils.isEmpty(getStr(edtDian))) {
            mToast("店名不能为空！");
            return;
        }
        if (position_id == 0) {
            mToast("职务不能为空！");
            return;
        }
        if (TextUtils.isEmpty(getStr(edtWorkYear))) {
            mToast("工作年限不能为空！");
            return;
        }
        if (TextUtils.isEmpty(getStr(edtSpeciality))) {
            mToast("特长不能为空！");
            return;
        }
        if (TextUtils.isEmpty(getStr(edtDream))) {
            mToast("梦想不能为空！");
            return;
        }
        if (TextUtils.isEmpty(getStr(edtWx))) {
            mToast("微信不能为空！");
            return;
        }
        if (TextUtils.isEmpty(getStr(edtQq))) {
            mToast("QQ不能为空！");
            return;
        }
        if (TextUtils.isEmpty(getStr(edtAddress))) {
            mToast("地址不能为空！");
            return;
        }
        httpCent.saveUserInfo(imagePath, getStr(edtUsername), sex, position_id, getStr(edtQq), getStr(edtWx), getStr(edtWorkYear), getStr(edtSpeciality), getStr(edtDream), getStr(edtAddress), this, MyState.requestThree);

    }

    @OnClick({R.id.iv_head, R.id.utils_sex, R.id.edt_work})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_head:
                MyUtils.startPhotoPicker(this, 1, 100);
                break;
            case R.id.utils_sex:
                showSex();
                break;
            case R.id.edt_work:
                openActivity(PositionActivity.class);
                break;
        }
    }

    /**
     * 选择职位
     *
     * @param bean
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(StaffBean bean) {
        edtWork.setMsg(bean.getName());
        position_id = bean.getId();
    }

    private void showSex() {
        SinglePicker<String> picker = new SinglePicker<>(this, pList);
        picker.setCanLoop(false);//不禁用循环
        picker.setLineVisible(true);
        picker.setShadowVisible(true);
        picker.setTextSize(18);
        picker.setSelectedIndex(1);
        picker.setWheelModeEnable(true);
        //启用权重 setWeightWidth 才起作用
        picker.setLabel(null);
        picker.setWeightEnable(true);
        picker.setWeightWidth(1);
        picker.setSelectedTextColor(0xFF279BAA);//前四位值是透明度
        picker.setUnSelectedTextColor(0xFF999999);
        picker.setOnItemPickListener(new OnItemPickListener<String>() {
            @Override
            public void onItemPicked(int index, String item) {
                if (pList.get(index).equals("男")) {
                    sex = 1;
                } else {
                    sex = 0;
                }
                if (sex == 0) {
                    utilsSex.setMsg("女");
                } else {
                    utilsSex.setMsg("男");
                }
            }
        });
        picker.show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 100:
                    if (data != null) {
                        ArrayList<String> photos = data.getStringArrayListExtra(PhotoPicker.KEY_SELECTED_PHOTOS);
                        if (photos != null) {
                            ImageTools.loadImage(this, photos.get(0), this, MyState.requestTwo);
                        }
                    }
            }
        }
    }
}
